package algorithm.tree;


import algorithm.TreeNode;

public class BalanceTree {

    public boolean isBalance(TreeNode node) {
        boolean[] res = new boolean[1];
        res[0] = true;
        getHeight(node, 1, res);
        return res[0];
    }

    public int getHeight(TreeNode node, int level, boolean[] res) {
        if (node == null) {
            return level;
        }
        int LH = getHeight(node.left, level + 1, res);
        if (!res[0]) {
            return level;
        }
        int RH = getHeight(node.right, level + 1, res);
        if (!res[0]) {
            return level;
        }
        if (Math.abs(LH - RH) > 1) {
            res[0] = false;
        }

        return Math.max(LH, RH);

    }
}
